﻿# https://blog.csdn.net/shrekz/article/details/52840157  里面评论提示有个问题，以后再改


$FtpFileList=@("/bdcdj6/X/default.htm")
$LocalFileList=@()
$DeleteFileList=@()
$Credentials=New-Object System.Net.NetworkCredential("webadmin","webadmin")
$ftphost="ftp://172.16.3.3"
$backupDir="E:\databackup\"

#ftp下载函数支持断点续传
function FtpDownload($fileName,$size){
$uri = $ftphost +"/"+$fileName
$outFilename=$backupDir+$fileName
try{
$outputStream=New-Object System.Io.FileStream($outFilename,"Append")
$ftpsize = [system.net.ftpwebrequest][system.net.webrequest]::create($uri)
$ftpsize.Credentials = $Credentials
$ftpsize.Method= "SIZE"
$ftpsize.ContentOffset = $size  
$response = $ftpsize.getresponse()
$response.Close()
$reqFTP = [system.net.ftpwebrequest][system.net.webrequest]::create($uri)
$reqFTP.Credentials = $Credentials
$reqFTP.KeepAlive = $false
$reqFTP.Method= "RETR"
$reqFTP.ContentOffset = $size
$response = $reqFTP.getresponse()
$ftpStream = $response.GetResponseStream()
$bufferSize = 2048  
$buffer = new-object System.Byte[] $bufferSize 
$readCount = $ftpStream.Read($buffer, 0, $bufferSize)   
while ($readCount -gt 0){  
$outputStream.Write($buffer, 0, $readCount)  
$readCount = $ftpStream.Read($buffer, 0, $bufferSize)  
}  
$ftpStream.Close()  
$outputStream.Close()  
$response.Close()  
return $true  
}  
catch [Exception]{ 
$_
return $false  
}  
finally{  
if ($ftpStream){$ftpStream.Close()}  
if ($outputStream){$outputStream.Close()}  
if ($response){$response.Close()}  
}}


#文件下载
function FtpDown($filename){
    $uri = $ftphost +"/"+$fileName
    $outFilename=$backupDir+$fileName
    $size = 0
    $flg=$true
do{
    if([io.File]::Exists($outFilename)){
    $outputStream=New-Object System.Io.FileStream($outFilename,"Open")
    $size = $outputStream.Length
    $outputStream.Close() 
}
    "开始下载文件"+$fileName+$(if($size -gt 0){"从$size`开始下载……"})
    $flg=FtpDownload $filename $size
}until($flg) 
}

#Ftp删除函数
function FtpDelete($filename){
try{
$uri = $ftphost +"/"+$fileName
$DeleteFTP = [system.net.ftpwebrequest][system.net.webrequest]::create($uri)
$DeleteFTP.Credentials = $Credentials
$DeleteFTP.Method = "DELE"
$response = $DeleteFTP.getresponse()
$datastream=$response.getresponsestream()
$stream = New-Object System.Io.StreamReader($datastream,[System.Text.Encoding]::UTF8)
$stream.ReadToEnd()
$stream.Close();
$datastream.Close();
$response.Close();
$filename+"删除成功……"
}
catch{$filename+"删除失败……"}
finally{  
if ($stream){$stream.Close()}  
if ($datastream){$datastream.Close()}  
if ($response){$response.Close()}  
}}

#文本转日期函数
function Str2Date($str){
$dateString = "20"+$str.substring(0,6)
$CultureInfo=[System.Globalization.CultureInfo]::CurrentCulture
[DateTime]::ParseExact($dateString, "yyyyMMdd",$CultureInfo)
}

##################################################################
#获取文件列表
$listFTP = [system.net.ftpwebrequest][system.net.webrequest]::create($ftphost)
$listFTP.Credentials = $Credentials
$listFTP.Method="NLST"
try{
$response = $listFTP.getresponse()
$stream = New-Object System.Io.StreamReader($response.getresponsestream(),[System.Text.Encoding]::UTF8)
while(-not $stream.EndOfStream){
$FtpFileList+=$stream.ReadLine()
}
$stream.Close()
$response.Close()}
catch{$filename+"删除失败……"}
finally{  
if ($stream){$stream.Close()}  
if ($response){$response.Close()}  
}  


#获取删除文件列表，保留服务器上的7个文件
$DeleteFileList=$FtpFileList|select -First ($FtpFileList.count-7)
#获取本地文件列表
$LocalFileList=dir $backupDir|select -Expand name
#获取最后一个文件名
$LastFtpFile=$FtpFileList|select -Last 1
$LastLocalFile=$LocalFileList|select -Last 1
#如果文件本地没有，并且日期大于本地文件的日期就下载这个文件
#if($LocalFileList -notcontains $LastFtpFile -and (Str2Date $LastFtpFile) -gt (Str2Date $LastLocalFile)){
$LastFtpFile+"开始下载……"
FtpDown $LastFtpFile 
#}


#如果删除列表里有删除文件
if($DeleteFileList){
    foreach($file in $DeleteFileList){
        $file+"开始删除……"
        FtpDelete $file
    }
}